package com.snap.vseries.adapter;


import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.view.View;

import com.snap.vseries.R;
import com.snap.vseries.base.BaseRecycleAdapter;
import com.snap.vseries.base.BaseViewHolder;
import com.snap.vseries.base.OnItemClickListener;
import com.snap.vseries.bean.ProgramSection;
import com.snap.vseries.dialog.TempDurationPickerDialog;

import java.util.List;
import java.util.Locale;

public class NewExpProgramAdapter extends BaseRecycleAdapter<ProgramSection> {

    private final FragmentManager fm;
    private OnItemClickListener onItemClickListener;

    public NewExpProgramAdapter(Context context, List<ProgramSection> dataList, FragmentManager fragmentManager) {
        super(context, dataList);
        this.fm = fragmentManager;
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public void bindData(BaseViewHolder holder, ProgramSection data, int position) {
        String name = String.format(Locale.CHINA, "Stage %02d", data.getSectionIndex());
        holder.setText(R.id.tv_section, name)
                .setText(R.id.tv_temp, data.getTemp() + "℃")
                .setText(R.id.tv_time, data.getDuration() + "s");
        holder.getView(R.id.temp_time_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TempDurationPickerDialog()
                        .setParam(data.getTemp(), data.getDuration(),
                                (temp, duration) -> {
                                    data.setTemp(temp);
                                    data.setDuration(duration);
                                    holder.setText(R.id.tv_temp, String.format("%s°C", temp))
                                            .setText(R.id.tv_time, String.format(Locale.CHINA, "%ds", duration));
                                })
                        .show(fm, "temp_duration_picker");
            }
        });
        holder.getView(R.id.clear_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(NewExpProgramAdapter.this, v, position);
            }
        });
        View lineTop = holder.getView(R.id.line_top);
        View lineBottom = holder.getView(R.id.line_bottom);

        lineTop.setVisibility(View.VISIBLE);
        lineBottom.setVisibility(View.VISIBLE);
        if (mDataList.size() == 1) {
            lineTop.setVisibility(View.INVISIBLE);
            lineBottom.setVisibility(View.INVISIBLE);
        }
        if (position == 0) {
            lineTop.setVisibility(View.INVISIBLE);
        } else if (position == mDataList.size() - 1) {
            lineBottom.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_experiment_method;
    }

    @Override
    public void updateData(List<ProgramSection> dataList) {
        super.updateData(dataList);
    }
}
